{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ab2b967f-6080-4f3a-8d1e-2b7eee2b3076",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "模型在训练集上的预测准确率为: 0.4326365837100665\n",
      "模型在测试集上的预测准确率为: 0.4325221097526062\n"
     ]
    }
   ],
   "source": [
    "#导入线性回归模型、糖尿病数据集及划分样本的方法\n",
    "from sklearn.linear_model import Ridge\n",
    "from sklearn.datasets import load_diabetes\n",
    "from sklearn.model_selection import train_test_split\n",
    "#将数据集划分为训练集和预测集\n",
    "x,y=load_diabetes().data,load_diabetes().target\n",
    "x_train,x_test,y_train,y_test=train_test_split(x,y,random_state=8)\n",
    "#训练模型\n",
    "model=Ridge()   #例2-5\n",
    "\n",
    "#model=Ridge(alpha=10)   #例2-6，提高alpha值，模型性能大大降低\n",
    "#model=Ridge(alpha=0.1)   #例2-7，alpha非常小，系统限制几乎忽略不计，结果接近线性回归\n",
    "\n",
    "model.fit(x_train,y_train)\n",
    "#计算模型的预测准确率\n",
    "r21=model.score(x_train,y_train)\n",
    "r22=model.score(x_test,y_test)\n",
    "print('模型在训练集上的预测准确率为:',r21)\n",
    "print('模型在测试集上的预测准确率为:',r22)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e1897d22-be8e-44bf-ac9d-19dc9febd3d8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
